package middleware

import (
	"github.com/gin-gonic/gin"

	"server/pkg/utils"
)

const SignKey = "abc"

//jwt中间件
func JWT() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.Request.Header.Get("x-token")
		if token == "" {
			c.JSON(200, gin.H{
				"result": "未登录",
			})
			//http.LoginHandler(c)
			c.Abort()
			return
		}

		j := utils.NewJwt([]byte(SignKey))
		parseToken, err := j.ParseToken(token)
		if err != nil {
			c.JSON(200, gin.H{
				"result": "token未解析",
			})
			c.Abort()
			return
		}
		if !parseToken.Valid {
			c.JSON(200, gin.H{
				"result": "token错误",
			})
			c.Abort()
			return
		}
		c.Next()
	}
}
